﻿// 文件路径：AyuBookmark.Common/Extensions/DapperExtensions.cs
using System.Collections.Generic;
using Dapper;

namespace AyuBookmark.Common.Extensions
{
    /// <summary>
    /// Dapper扩展方法
    /// </summary>
    public static class DapperExtensions
    {
        /// <summary>
        /// 将字典合并到DynamicParameters
        /// </summary>
        public static DynamicParameters MergeParameters(this DynamicParameters target, IDictionary<string, object> source)
        {
            foreach (var item in source)
            {
                target.Add(item.Key, item.Value);
            }
            return target;
        }

        /// <summary>
        /// 将匿名对象转换为DynamicParameters
        /// </summary>
        public static DynamicParameters ToDynamicParameters(this object parameters)
        {
            var dynamicParams = new DynamicParameters();
            if (parameters != null)
            {
                foreach (var prop in parameters.GetType().GetProperties())
                {
                    dynamicParams.Add(prop.Name, prop.GetValue(parameters));
                }
            }
            return dynamicParams;
        }
    }
}